package cn.com.thingo.hyintelligent.ui.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;

import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.videogo.errorlayer.ErrorInfo;
import com.videogo.openapi.EZConstants;
import com.videogo.openapi.EZOpenSDK;
import com.videogo.openapi.EZPlayer;
import com.videogo.openapi.bean.EZDeviceInfo;
import com.videogo.util.LocalInfo;
import com.videogo.util.LogUtil;
import com.videogo.util.Utils;
import com.videogo.widget.RingView;

import java.util.List;

import cn.com.thingo.hyintelligent.R;
import cn.com.thingo.hyintelligent.model.db.EzMonitorRecEntity;
import cn.com.thingo.hyintelligent.util.SPUtils;
import cn.com.thingo.hyintelligent.widget.PlayerLoadingTextView;

public class MonitorRecAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {
    public static final int TYPE_DATA = 0;
    public static final int TYPE_ADD = 1;
    private SPUtils mSPUtils;
    private HandlerCallback mHandlerCallback;

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public MonitorRecAdapter(List<MultiItemEntity> data) {
        super(data);
        addItemType(TYPE_DATA, R.layout.item_player_rec);
        addItemType(TYPE_ADD, R.layout.item_player_add);
        mSPUtils = SPUtils.newInstance();
    }

    @Override
    protected void convert(BaseViewHolder helper, final MultiItemEntity item) {
        switch (item.getItemType()) {
            case TYPE_DATA:
                EzMonitorRecEntity monitorRecEntity = (EzMonitorRecEntity) item;
                EZDeviceInfo ezDeviceInfo = monitorRecEntity.getEZDeviceInfo();
                TextView tvName = helper.getView(R.id.realplay_name);
                String deviceSerial = ezDeviceInfo.getDeviceSerial();
                String verifyCode = mSPUtils.getString(deviceSerial);
                TextView tvTip = helper.getView(R.id.realplay_tip_tv);
                PlayerLoadingTextView viewLoading = helper.getView(R.id.realplay_loading);
                ImageView ivPlay = helper.getView(R.id.realplay_play_iv);
                SurfaceView sv = helper.getView(R.id.realplay_sv);
                tvName.setText(ezDeviceInfo.getDeviceName());
                final int DEVICE_ONLINE = 1;
                final int DEVICE_OFFLINE = 2;
                final EZPlayer ezPlayer = EZOpenSDK.getInstance().createPlayer(deviceSerial, ezDeviceInfo.getCameraInfoList().get(0).getCameraNo());
                monitorRecEntity.setEZPlayer(ezPlayer);
                switch (ezDeviceInfo.getStatus()) {
                    case DEVICE_ONLINE:
                        if (verifyCode != null) {
                            monitorRecEntity.setVerifyCode(verifyCode);
                            SurfaceHolder holder = sv.getHolder();
                            holder.addCallback(new SurfaceHolder.Callback() {
                                @Override
                                public void surfaceCreated(SurfaceHolder holder) {
                                    if (ezPlayer != null) {
                                        ezPlayer.setSurfaceHold(holder);
                                    }
                                }

                                @Override
                                public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

                                }

                                @Override
                                public void surfaceDestroyed(SurfaceHolder holder) {
                                    if (ezPlayer != null) {
                                        ezPlayer.setSurfaceHold(null);
                                    }
                                }
                            });
                            if (mHandlerCallback != null) {
                                Handler handler = mHandlerCallback.callback(monitorRecEntity, helper.itemView);
                                ezPlayer.setHandler(handler);
                            }
                            ezPlayer.setPlayVerifyCode(verifyCode);
                            ezPlayer.startRealPlay();
                        } else {
                            tvTip.setVisibility(View.GONE);
                            ivPlay.setVisibility(View.VISIBLE);
                            viewLoading.setVisibility(View.GONE);
                        }

                        break;
                    case DEVICE_OFFLINE:
                        tvTip.setVisibility(View.VISIBLE);
                        ivPlay.setVisibility(View.GONE);
                        viewLoading.setVisibility(View.GONE);
                        break;
                }

                break;
        }
        RelativeLayout relativeLayout = helper.getView(R.id.realplay_play_rl);
relativeLayout.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()){
            case  MotionEvent.ACTION_DOWN:
                v.requestFocus();
                break;
                case MotionEvent.ACTION_UP:
                    v.performClick();
                    break;
            case MotionEvent.ACTION_OUTSIDE:
                return true;
        }

        return true;
    }
});


    }


    public interface HandlerCallback {
        Handler callback(EzMonitorRecEntity entity, View view);
    }

    public void setHandlerCallback(HandlerCallback callback) {
        mHandlerCallback = callback;
    }
}
